package com.lisx.module_time_block.adapter;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.fenghuajueli.lib_data.entity.db.DayPlanDbEntity;
import com.lisx.module_time_block.R;
import com.lisx.module_time_block.bean.NewBlockIconBean;
import com.lisx.module_time_block.databinding.ItemTimeBlockListBinding;

import java.util.ArrayList;
import java.util.List;

public class TimeBlockItemAdapter extends RecyclerView.Adapter<TimeBlockItemAdapter.MyViewHolder> {

    List<NewBlockIconBean> iconData;
    List<DayPlanDbEntity> list = new ArrayList<>();
    Context context;

    public TimeBlockItemAdapter(Context context, List<NewBlockIconBean> iconData) {
        this.context = context;
        this.iconData = iconData;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemTimeBlockListBinding binding = ItemTimeBlockListBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
        return new MyViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        DayPlanDbEntity entity = list.get(position);
        holder.binding.ivRes.setImageResource(iconData.get(entity.getResPosition()).res);
        holder.binding.tvName.setText(entity.getPlan_name());
        holder.binding.tvCount.setText(entity.getDaka_count() + "天");
        holder.binding.cardView.setCardBackgroundColor(Color.parseColor(entity.getColor()));

        holder.binding.cardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onItemClick(position,entity);
                }
            }
        });

        holder.binding.cardView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (listener != null) {
                    listener.onItemLongClick(entity);
                }
                return false;
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public void setData(List<DayPlanDbEntity> list) {
        this.list.clear();
        this.list.addAll(list);
        notifyDataSetChanged();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {

        private final ItemTimeBlockListBinding binding;

        public MyViewHolder(@NonNull ItemTimeBlockListBinding itemView) {
            super(itemView.getRoot());
            binding = itemView;
        }
    }

    public interface OnItemClickListener {
        void onItemClick(int position, DayPlanDbEntity entity);
        void onItemLongClick(DayPlanDbEntity entity);
    }

    OnItemClickListener listener;

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }
}
